// 此js  用于检测token是否携带

import axios from 'axios'  //引入axios
// var token = "";
//创建默认实例
const instanceToken = axios.create({
  baseURL:'http://localhost:8989',
  timeout:1000,
  // `headers` 是即将被发送的自定义请求头
  // headers: { 'token': window.sessionStorage.getItem("token")},//token用window.sessionStorage.getItem("token")为null，可能无法获取
});


//请求拦截器
instanceToken.interceptors.request.use(config=>{
  console.log("请求拦截器");
  console.log(window.sessionStorage.getItem("token"));
  config.headers.common['token'] = window.sessionStorage.getItem("token");
  return config;
})
//响应拦截器
instanceToken.interceptors.response.use(response=>{
  console.log(response)
  console.log(response.data.message)
  let t = response.data.message
  if(t=="NoToken"||t=="NoFoundUser"||t=="TokenFail"||t=="IllegalToken"){
    console.log("响应拦截器")
    window.location.href="http://www.wanmait.com";
  }
  return response;
},err=>{
  console.log("响应出现错误时进入的拦截器");
});
//暴露instance实例对象
export default instanceToken;




